import axios from 'axios'

import { ElMessage } from 'element-plus'

import NProgress from 'plugins/nprogress'

import store from '@/store'

const service = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 5000,
})

service.interceptors.request.use(
    (config) => {
        NProgress.start()
        config.headers['Authorization'] = 'my token'
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    (response) => {
        NProgress.done()
        const res = response.data
        if (res.code !== 0) {
            ElMessage.error({
                message: res.message || 'Error',
                duratons: 5 * 1000,
            })
        }
        return response.data
    },
    (error) => {
        NProgress.done()
        return Promise.reject(error)
    }
)

export default service
